<?php

/**
 * debitMemo actions.
 *
 * @package    erp-crm
 * @subpackage debitMemo
 * @author     Milwell H. Sia
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class debitMemoActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request){}
  
  public function executeCreate(sfWebRequest $request){
    $invoice_iden = $request->getParameter('invoice_iden');
    $this->invoice = InvoicePeer::retrieveByPk($invoice_iden);
    $this->forward404Unless($this->invoice);
    $customer_iden = $this->invoice->getCustomerIden();
    $this->accountCustomer = AccountCustomerPeer::retrieveByPk($customer_iden);
    $this->forward404Unless($this->accountCustomer);
    $this->documentForm = new DocumentForm();
    $this->documentForm->setDefault('owner_iden',$this->invoice->getDocument()->getOwnerIden());
    $this->documentForm->setOwnerReadOnly();
    $this->debitMemoForm = new DebitMemoForm();
    $this->debitMemoForm->setDefault('customer_iden',$customer_iden);
    $this->debitMemoForm->setDefault('invoice_iden',$invoice_iden);
    $this->contact = $this->accountCustomer->getContact();
    $this->error_message = '';
    if(sfConfig::get('mod_debitmemo_number_auto'))
      $this->debitMemoForm->setNumberReadOnly();
    $this->setLayout(false);
  }
  
  public function executeInsert(sfWebRequest $request){
    $document_params = $request->getParameter('document');
    $debit_memo_params = $request->getParameter('debit_memo');
    
    $document_params['document_type']='DEBIT MEMO';
    $document_params['void']='N';
    $document_params['void_date']=null;
    
    $document_params['document_iden'] = myStringGenerator::getIden();
    $debit_memo_params['debit_iden'] = $document_params['document_iden'];
    
    $this->documentForm = new DocumentForm();
    $this->debitMemoForm = new DebitMemoForm();
    
    if((sfConfig::get('mod_debitmemo_number_auto'))&&(! $debit_memo_params['debit_number'])){
      $debit_memo_number = ListDocumentTypesPeer::getNextId('DEBIT',$debit_memo_params['debit_type']);
      if ($debit_memo_number){
        $debit_memo_params['debit_number'] = vsprintf(sfConfig::get('mod_debitmemo_number_format'),$debit_memo_number);
        $this->debitMemoForm->setNumberTypeReadOnly();
      }
    }
    
    $this->documentForm->bind($document_params);
    $this->debitMemoForm->bind($debit_memo_params);
    
    $this->error_message = '';
    if((sfConfig::get('mod_debitmemo_number_strict'))&&($this->isUnique(null,$debit_memo_params['debit_type'],$debit_memo_params['debit_number'])>0)){
      $this->invoice = InvoicePeer::retrieveByPk($debit_memo_params['invoice_iden']);
      $this->accountCustomer = AccountCustomerPeer::retrieveByPk($debit_memo_params['customer_iden']);
      $this->forward404Unless($this->accountCustomer);
      $this->contact = $this->accountCustomer->getContact();
      $this->setLayout(false);
      $this->error_message = $debit_memo_params['debit_type'].'-'.$debit_memo_params['debit_number'].' already exists.';
      return $this->setTemplate('create');
    }
    
    if(!($this->documentForm->isValid() && $this->debitMemoForm->isValid())){
      $this->invoice = InvoicePeer::retrieveByPk($debit_memo_params['invoice_iden']);
      $this->accountCustomer = AccountCustomerPeer::retrieveByPk($debit_memo_params['customer_iden']);
      $this->forward404Unless($this->accountCustomer);
      $this->contact = $this->accountCustomer->getContact();
      $this->setLayout(false);
      return $this->setTemplate('create');
    }
    
    $con = Propel::getConnection(LogTransactionPeer::DATABASE_NAME,Propel::CONNECTION_WRITE);
    $con->beginTransaction();
    try{   
      $this->documentForm->save();
      $this->debitMemoForm->save();
      $log_transaction = new LogTransaction();
      $log_transaction->setTransactionType('DEBIT MEMO');
      $log_transaction->setIden1($this->debitMemoForm->getObject()->getCustomerIden());
      $log_transaction->setType1('CLIENT');
      $log_transaction->setIden2($this->debitMemoForm->getObject()->getDebitIden());
      $log_transaction->setType2('DEBIT MEMO');
      $log_transaction->setIden3($this->debitMemoForm->getObject()->getInvoiceIden());
      $log_transaction->setType3('INVOICE');
      $log_transaction->setDebit($this->debitMemoForm->getObject()->getAmount());
      $log_transaction->setDocumentIden($this->debitMemoForm->getObject()->getDebitIden());
      $log_transaction->save();
      $log_transaction->setTransactionType('INVOICE');
      $log_transaction->setIden1($this->debitMemoForm->getObject()->getCustomerIden());
      $log_transaction->setType1('CLIENT');
      $log_transaction->setIden2($this->debitMemoForm->getObject()->getInvoiceIden());
      $log_transaction->setType2('INVOICE');
      $log_transaction->setIden3($this->debitMemoForm->getObject()->getDebitIden());
      $log_transaction->setType3('DEBIT MEMO');
      $log_transaction->setDebit($this->debitMemoForm->getObject()->getAmount());
      $log_transaction->setDocumentIden($this->debitMemoForm->getObject()->getDebitIden());
      $log_transaction->save();
      $con->commit();
      $this->redirect('debitMemo/edit?debit_iden='.$this->debitMemoForm->getObject()->getDebitIden());
    }catch(Exception $e){
      $con->rollBack();
      $request->getParameterHolder()->set('message','P'.$e->getMessage().'Y');
      $this->forward('main','error');
    }
    
  }
    
  public function executeEdit(sfWebRequest $request){
    $debitMemo = DebitMemoPeer::retrieveByPk($request->getParameter('debit_iden'));
    $this->forward404Unless($debitMemo);
    $document = $debitMemo->getDocument();
    $this->documentForm = new DocumentForm($document);
    $this->debitMemoForm = new DebitMemoForm($debitMemo);
    $this->accountCustomer = $debitMemo->getAccountCustomer();
    $this->contact = $this->accountCustomer->getContact();
    $this->invoice = $debitMemo->getInvoice();
    $this->setLayout(false);
    $this->setTemplate('create');
    $this->error_message = '';
    if(sfConfig::get('mod_debitmemo_number_auto'))
      $this->debitMemoForm->setNumberTypeReadOnly();
  }
  
  public function executeSearch(sfWebRequest $request){
    $this->debitMemoSearchForm = new myDebitMemoSearchForm();
  }
  
  public function executeDoSearch(sfWebRequest $request){
    $search_keys = $request->getParameter('skey_debit');
    $debitMemoSearchForm = new myDebitMemoSearchForm();
    $debitMemoSearchForm->bind($search_keys);

    $c = new Criteria();
    if($debitMemoSearchForm->getValue('debit_type'))
      $c->add(DebitMemoPeer::DEBIT_TYPE,$debitMemoSearchForm->getValue('debit_type'));
    if($debitMemoSearchForm->getValue('debit_number_from'))
      $c->addAnd(DebitMemoPeer::DEBIT_NUMBER,$debitMemoSearchForm->getValue('debit_number_from'),Criteria::GREATER_EQUAL);
    if($debitMemoSearchForm->getValue('debit_number_to'))
      $c->addAnd(DebitMemoPeer::DEBIT_NUMBER,$debitMemoSearchForm->getValue('debit_number_to'),Criteria::LESS_EQUAL);
    if($debitMemoSearchForm->getValue('invoice_type'))
      $c->add(InvoicePeer::INVOICE_TYPE,$debitMemoSearchForm->getValue('invoice_type'));
    if($debitMemoSearchForm->getValue('invoice_number_from'))
      $c->addAnd(InvoicePeer::INVOICE_ID,$debitMemoSearchForm->getValue('invoice_number_from'),Criteria::GREATER_EQUAL);
    if($debitMemoSearchForm->getValue('invoice_number_to'))
      $c->addAnd(InvoicePeer::INVOICE_ID,$debitMemoSearchForm->getValue('invoice_number_to'),Criteria::LESS_EQUAL);
    if($debitMemoSearchForm->getValue('debit_date_from'))
      $c->addAnd(DocumentPeer::DOCUMENT_DATE,$debitMemoSearchForm->getValue('debit_date_from'),Criteria::GREATER_EQUAL);
    if($debitMemoSearchForm->getValue('debit_date_to'))
      $c->addAnd(DocumentPeer::DOCUMENT_DATE,$debitMemoSearchForm->getValue('debit_date_to'),Criteria::LESS_EQUAL);
    if($debitMemoSearchForm->getValue('customer_name'))
      $c->add(ContactPeer::LIST_NAME,$debitMemoSearchForm->getValue('customer_name'),Criteria::GREATER_EQUAL);
    if($debitMemoSearchForm->getValue('show_void_only')!='Y')
      $c->add(DocumentPeer::VOID,'N');
    else
      $c->add(DocumentPeer::VOID,'Y');
    $c->addJoin(DebitMemoPeer::DEBIT_IDEN,DocumentPeer::DOCUMENT_IDEN);
    $c->addJoin(DebitMemoPeer::CUSTOMER_IDEN,ContactPeer::IDEN);
    $c->addJoin(DebitMemoPeer::INVOICE_IDEN,InvoicePeer::INVOICE_IDEN);
    $this->debitMemos = DebitMemoPeer::doSelect($c);
  }
  
  public function executeVoid(sfWebRequest $request){
    $debitMemo = DebitMemoPeer::retrieveByPk($request->getParameter('debit_iden'));
    $this->forward404Unless($debitMemo);
    $document = $debitMemo->getDocument();
    $document->void();
    return $this->renderText(json_encode(array('debit_iden'=>$debitMemo->getDebitIden(),)));
  }
  
  public function executeGetTd(sfWebRequest $request){
    $debitMemo = DebitMemoPeer::retrieveByPk($request->getParameter('debit_iden'));
    return $this->renderPartial('debit_grid_td',array('debitMemo'=>$debitMemo,));
  }

  private function isUnique($debit_iden,$debit_type,$debit_number){
    $c = new Criteria();
    if ($debit_iden)
      $c->addAnd(DebitMemoPeer::DEBIT_IDEN,$debit_iden,Criteria::NOT_EQUAL);
    $c->addAnd(DebitMemoPeer::DEBIT_TYPE,$debit_type);
    $c->addAnd(DebitMemoPeer::DEBIT_NUMBER,$debit_number);
    $c->addJoin(DocumentPeer::DOCUMENT_IDEN,DebitMemoPeer::DEBIT_IDEN);
    $c->addAnd(DocumentPeer::VOID,'N');
    return DebitMemoPeer::doCount($c);
  }
  
  public function executeCheckId(sfWebRequest $request){
    $debit_iden = $request->getParameter('debit_iden');
    $debit_type = $request->getParameter('debit_type');
    $debit_number = $request->getParameter('debit_number');
    $count = $this->isUnique($debit_iden,$debit_type,$debit_number);
    return $this->renderText(json_encode(array('count'=>$count,)));
  }


}
